package com.lpc.carinfo;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.RelativeLayout;

import com.lpc.carinfo.activity.AddCarActivity;
import com.lpc.carinfo.adapter.CarInfoAdapter;
import com.lpc.carinfo.bean.CarInfoBean;
import com.lpc.carinfo.db.MyDB;
import com.lpc.carinfo.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private RelativeLayout toolbar;
    private GridView lv;
    private CarInfoAdapter adapter;
    private Button add;
    private Button query;
    private EditText content;
    private List<CarInfoBean> lists;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
    }

    private void initView() {

        toolbar = findViewById(R.id.toolbar);
        lv = findViewById(R.id.lv);

        content = findViewById(R.id.content);

        add = findViewById(R.id.add);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent i = new Intent(MainActivity.this, AddCarActivity.class);
                startActivityForResult(i, 0);
            }
        });
        query = findViewById(R.id.query);
        query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                query(content.getText().toString());
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        query("");
    }

    private void query(String content) {

        lists.clear();
        if(TextUtils.isEmpty(content)){
            lists.addAll(MyDB.getInstace().query());
        }else{
            lists.addAll(MyDB.getInstace().find(content));
        }


        if(lists == null){
            lists = new ArrayList<CarInfoBean>();
        }

        CarInfoBean bean = new CarInfoBean();
        bean.setCarSeries("车型");
        bean.setEngineOil("机油");
        bean.setJiLv("机滤");
        bean.setKongLv("空滤");
        bean.setKongTiaoLv("空调滤");
        bean.setQilv("汽滤");
        bean.setHuosaihua("火花塞");

        lists.add(0, bean);

        if(lists.size() <= 1){
            ToastUtils.showShortToast("没有符合条件的目录");
        }

        adapter.notifyDataSetChanged();
    }

    private void initData() {

        lists = MyDB.getInstace().query();

        CarInfoBean bean = new CarInfoBean();
        bean.setCarSeries("车型");
        bean.setEngineOil("机油");
        bean.setJiLv("机滤");
        bean.setKongLv("空滤");
        bean.setKongTiaoLv("空调滤");
        bean.setQilv("汽滤");
        bean.setHuosaihua("火花塞");

        lists.add(0, bean);

        adapter = new CarInfoAdapter(this, lists);
        lv.setAdapter(adapter);

    }
}
